package com.wlient.basic.pachong;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author Wlient
 * @date 2020/9/15 14:54
 */
public class MoreMovice {

    public void getMovieDetail(String url) throws Exception {

        HttpEntity responseEntity = getHttpEntity(url);
        if (responseEntity != null) {
            Document doc = Jsoup.parse(EntityUtils.toString(responseEntity));
            String data = doc.getElementsByTag("body").text();
//            System.out.println(data);
            JSONObject jsonObject = JSONObject.parseObject(data);
            JSONArray jsonArray = jsonObject.getJSONArray("subjects");
            jsonArray.stream().forEach(m -> {
                JSONObject mov = JSONObject.parseObject(m.toString());
                Movie movie = new Movie();
                movie.setMoviename(mov.getString("title"));
                movie.setRate(mov.getString("rate"));
                movie.setUrl(mov.getString("cover"));
//                System.out.println(movie);
                try {
                    write(mov.getString("cover"), mov.getString("title"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
    }

    public HttpEntity getHttpEntity(String url) throws IOException {
        Document doc = null;
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.48");
        // 响应模型
        CloseableHttpResponse response = null;

        // 由客户端执行(发送)Get请求
        response = httpClient.execute(httpGet);
        // 从响应模型中获取响应实体
        return response.getEntity();
    }

    public void write(String urlString, String title) throws Exception {
        // 构造URL
        URL url = new URL(urlString);
        // 打开连接
        URLConnection con = url.openConnection();
        // 输入流
        InputStream is = con.getInputStream();
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        // 输出的文件流
        String filename = "豆瓣热门电影top250/" + title + ".jpg";
        //下载路径及下载图片名称
        File file = new File(filename);
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        FileOutputStream os = new FileOutputStream(file, true);
        // 开始读取
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        // 完毕，关闭所有链接
        os.close();
        is.close();
    }
}
